package com.hanhanxiaochong.zhongxiangchuanmei.widget;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.jakewharton.rxbinding.view.RxView;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Description:
 * Created by RockPhoenixHIAPAD on 2018/1/11 on 16:36.
 */

public class ConfirmDialog extends DialogFragment {

    @BindView(R.id.tv_confirm_content)
    TextView tvConfirmContent;
    @BindView(R.id.tv_cancel)
    AppCompatTextView tvCancel;
    @BindView(R.id.tv_confirm)
    AppCompatTextView tvConfirm;
    private Unbinder unbinder;
    private String content;

    public static ConfirmDialog newInstance(Bundle bundle) {
        ConfirmDialog deleteDialog = new ConfirmDialog();
        deleteDialog.setArguments(bundle);
        return deleteDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        content = arguments.getString("content");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View dialog = ResourcesUtils.inflate(R.layout.dialog_confirm);
        unbinder = ButterKnife.bind(this, dialog);
        tvConfirmContent.setText(content);
        //取消
        RxView.clicks(tvCancel)
                .throttleFirst(1000, TimeUnit.SECONDS)
                .subscribe(o -> dismiss());
        //确定
        RxView.clicks(tvConfirm)
                .throttleFirst(1000, TimeUnit.SECONDS)
                .subscribe(o -> {
                    listener.onConfirm();
                    dismiss();
                });
        return dialog;
    }

    private DialogConfirmListener listener;

    public interface DialogConfirmListener {
        void onConfirm();
    }

    public void setOnConfirmListener(DialogConfirmListener listener) {
        this.listener = listener;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
